﻿using aier.business.logic;
using aier.dataaccess.model;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    [RoutePrefix("api/Reservation")]
    public class ReservationController : ApiController
    {
        public ReservationBll reservationBll { get; set; }
        public ILog log { get; set; }

        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }

        [ActionName("AddReservation")]
        [HttpPost]
        public IHttpActionResult AddReservation([FromBody]Reservation reservation) {

            if(!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

           
            return Ok(reservationBll.InsertReservation(reservation));
        }

     /// <summary>
     /// 分页获取预约记录
     /// </summary>
     /// <param name="startdate"></param>
     /// <param name="enddate"></param>
     /// <param name="isHospital"></param>
     /// <param name="pageIndex"></param>
     /// <param name="pageSize"></param>
     /// <returns></returns>
        [HttpGet]
        [ActionName("QueryReservationByIstohospital")]
        public IHttpActionResult QueryReservationByIstohospital(string startdate,string enddate,int isHospital,int pageIndex, int pageSize)
        {
            try
            {
                PageResult<ReservationView> pageResult = reservationBll.QueryReservationByIstohospital(startdate, enddate, isHospital, pageIndex, pageSize);
                return Ok<PageResult<ReservationView>>(pageResult);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
            
        }

        [HttpGet]
        [ActionName("FetchReservationById")]
        public IHttpActionResult FetchReservationById(Guid reservationid)
        {
            ReservationView reservationView = reservationBll.FetchReservationById(reservationid);
            return Ok<ReservationView>(reservationView);
        }

        

        /// <summary>
        /// 根据潜在顾客ID查询预约记录
        /// 条件：潜在顾客ID
        /// ReservationServiceImpl/QueryReservationByLeadId
        /// </summary>
        /// <param name="leadId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryReservationByLeadId")]
        public IHttpActionResult QueryReservationByLeadId(Guid leadId)
        {
            ReservationView reservationView = reservationBll.QueryReservationByLeadId(leadId);
            return Ok<ReservationView>(reservationView);
        }

        /// <summary>
        /// 根据潜在顾客ID查询预约记录
        /// 条件：潜在顾客ID
        /// ReservationServiceImpl/QueryReservationByLeadId
        /// </summary>
        /// <param name="leadId"></param>
        /// <param name="new_insurancetype">医保类型</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryReservationData")]
        public IHttpActionResult QueryReservationByLeadId(Guid leadId, bool new_insurancetype)
        {
            ReservationView reservationView = reservationBll.QueryReservationByLeadId(leadId,true);
            return Ok<ReservationView>(reservationView);
        }


        /// <summary>
        /// 根据潜在顾客ID查询预约记录列表
        /// 条件：潜在顾客ID
        /// ReservationServiceImpl/QueryReservationByLeadId
        /// </summary>
        /// <param name="leadId"></param>
        /// <param name="new_insurancetype">医保类型</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QueryReservationListByLeadId")]
        public IHttpActionResult QueryReservationListByLeadId(Guid leadId, bool new_insurancetype)
        {
            var reservationViewList = reservationBll.QueryReservationByLeadIdList(leadId, true);
            return Ok(reservationViewList);
        }

        /// <summary>
        /// 根据潜在顾客ID,筛查ID查询预约记录
        /// </summary>
        /// <param name="leadid">潜客ID</param>
        /// <param name="scid">筛查ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryReservationByLeadIdAndScid")]
        public IHttpActionResult QueryReservationByLeadIdAndScid(Guid leadid, Guid scid)
        {
            IEnumerable<ReservationView> reservationViews = reservationBll.QueryReservationByLeadIdAndScid(leadid, scid);
            return Ok<IEnumerable<ReservationView>>(reservationViews);
        }

        /// <summary>
        /// 创建预约信息
        /// 原程序 ReservationServiceImpl/createrReservation
        /// </summary>
        /// <param name="reservationModel"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("CreaterReservation")]
        public IHttpActionResult CreaterReservation(Reservation reservationModel)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            ResResult resResult  = reservationBll.CreateReservation(reservationModel, ownerId);
            return Ok(resResult);
        }

        [HttpPost]
        [Authorize]
        [ActionName("UpdateReservation")]
        public IHttpActionResult UpdateReservation(Reservation reservationModel)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            ResResult resResult = reservationBll.UpdateReservation(reservationModel, ownerId);
            return Ok(resResult);
        }

        /// <summary>
        /// 更新预约时间
        /// 原程序 ReservationServiceImpl/updateReservationDate
        /// </summary>
        /// <param name="reservationModel"></param>
        /// <returns></returns>
        [ActionName("UpdateReservationDate")]
        [HttpPost]
        public IHttpActionResult UpdateReservationDate(Reservation reservationModel)
        {
            ResResult resResult = reservationBll.UpdateReservationDate(reservationModel);
            return Ok(resResult);
        }

    }
}
